package features.advance.jdk.concurrent;

/**
 *  Volatile功能演示
 * @author LIN
 * @date 2021-10-13 14:26
 */
public class VolatileDemo {
    public static void main(String[] args) {
        Demo1 demo1 = new Demo1();
        demo1.start();
        for (;;){
            synchronized (demo1){
                if(demo1.isFlag()){
                    System.out.println("进入if分支");
                }
            }

        }

    }

}

class Demo1 extends Thread{

//   private volatile boolean flag = false;
   private boolean flag = false;

   public boolean isFlag(){
       return this.flag;
   }
    @Override
    public void run() {
        try {
            Thread.sleep(1000);
            flag = true;
            System.out.println("flag="+flag);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
